package cn.tedu.emp.comtroller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController

public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    //请求到该节点 /cluster 传递参数 key value
    //方法中将key写入到redis集群中
    @RequestMapping("/cluster")
    public String setAndGet(String key,String value){
        //redisTemplate封装底层client jedis lettuce
        //redisTemplate

        //设置超时的时长
        redisTemplate.expire(key,2, TimeUnit.HOURS);
        //拿取剩余时间
        Long ttl = redisTemplate.getExpire(key);
        //设置超时的时间
        //redisTemplate.expireAt()
        System.out.println("超时时间还剩"+ttl+"分钟");
        Boolean exist = redisTemplate.hasKey(key);//exists
        System.out.println(key+(exist?"存在":"不存在"));
        redisTemplate.delete(key);
        ValueOperations<String, String> opsForString = redisTemplate.opsForValue();
        opsForString.increment("num",100);
        opsForString.set(key, value);

        return key;
    }

    @RequestMapping("/Test")
    public String redisTest(String key,String value){
        //判断是否存在
          Boolean judge = redisTemplate.hasKey(key);
            /*redisTemplate.delete(key);*/

        String s = redisTemplate.randomKey();
        return s;
    }











}
